﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using System.Text.RegularExpressions;
using System.IO;

namespace iTools
{
    /// <summary>
    /// WinHosts.xaml 的交互逻辑
    /// </summary>
    public partial class WinHosts : Window
    {
        public WinHosts()
        {
            InitializeComponent();
        }

        string path_hosts = @"C:\Windows\System32\drivers\etc\hosts";
        Regex reg = new Regex(@"^\s*\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\s+");  

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            List<string> list = new List<string>();

            if (File.Exists(this.path_hosts))
            {
                using (StreamReader sr = new StreamReader(this.path_hosts, Encoding.Default))
                {
                    while (!sr.EndOfStream)
                    {
                        string line = sr.ReadLine();
                        if (reg.IsMatch(line))
                        {
                            list.Add(line);
                        }
                    }
                }
            }

            var rs = from n in list
                     where n.StartsWith("127")
                     select n;
            foreach (var r in rs)
            {
                MessageBox.Show(r);
            }
        }
    }
}
